﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlowBasis.Data;
using System.Data.Metadata.Edm;

namespace FlowBasis.EntityFramework
{
    public class EntityFrameworkPrimaryKeyService : IEntityPrimaryKeyService
    {
        private EntityFrameworkMetadataService metadataService;

        public EntityFrameworkPrimaryKeyService(EntityFrameworkMetadataService metadataService)
        {
            this.metadataService = metadataService;
        }

        public IList<string> TryGetPrimaryKeyPropertyNames(Type type)
        {       
            EntityType entityType = this.metadataService.TryGetEntityTypeForClrType(type);
            if (entityType != null)
            {
                var keyMembers = entityType.KeyMembers;
                int count = keyMembers.Count;

                List<string> primaryKeyPropertyNames = new List<string>(count);
                for (int co = 0; co < count; co++)
                {
                    primaryKeyPropertyNames.Add(keyMembers[co].Name);
                }

                return primaryKeyPropertyNames.AsReadOnly();
            }

            return null;
        }

        public object[] TryGetPrimaryKeyValues(object entityInstance)
        {
            throw new NotImplementedException();
        }

        public bool AssignPrimaryKeyValues(object entityInstance, object[] newPrimaryKeyValues)
        {
            throw new NotImplementedException();
        }       
    }
}
